class Solution:
    def longestSubarray(self, nums: list) -> int:
        ans = 0
        left = 0
        right = 0
        zero = 0
        one = 0
        while right < len(nums):
            if nums[right] == 0:
                zero += 1
            else:
                one += 1
            while zero > 1:
                if nums[left] == 0:
                    zero -= 1
                else:
                    one -= 1
                left += 1
            right += 1
            ans = max(one, ans)

        if one == len(nums):
            return ans - 1
        return ans
